package com.zerui.manager.web;

import com.zerui.authority.annotation.Authorize;
import com.zerui.cola.dto.PageResponse;
import com.zerui.cola.dto.Response;
import com.zerui.cola.dto.SingleResponse;
import com.zerui.devicemanager.provider.dto.PositionNodeDTO;
import com.zerui.devicemanager.provider.dto.SummaryDTO;
import com.zerui.manager.common.util.IpUtil;
import com.zerui.manager.web.cmd.*;
import com.zerui.manager.web.vo.DevicePageVO;
import com.zerui.manager.web.vo.feign.DeviceInfoFeignVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.List;

/**
 * @Author wh
 * @create_time 2023-08-23 10:21
 */
@Validated
@Api(value = "设备管理", tags = "设备管理")
@RestController
@RequestMapping("/api/v1/device")
@Authorize(value = {"access-control-manager:admin-device"})
public class DeviceController {

	@Resource
	private DeviceService deviceService;
	@Resource
	private DeviceTouchService deviceTouchService;

	@ApiOperation(value = "空间树", tags = "门禁设备管理")
	@GetMapping("/getProductTree")
	public SingleResponse<List<PositionNodeDTO>> getProductTree() {
		return deviceService.getProductTree();
	}

	@ApiOperation(value = "查询门禁设备列表", tags = "门禁设备管理")
	@PostMapping("/getAccessControlDeviceList")
	public SingleResponse<List<DeviceInfoFeignVO>> getAccessControlDeviceList(@RequestBody DeviceInfoCmd deviceInfoCmd) {
		return deviceTouchService.getAccessControlDeviceList(deviceInfoCmd);
	}

	@ApiOperation(value = "查询设备列表", tags = "门禁设备管理")
	@PostMapping("/getDeviceList")
	public PageResponse<DevicePageVO> getPageDeviceList(@RequestBody DeviceInfoCmd deviceInfoCmd) {
		return deviceTouchService.getPageDeviceList2(deviceInfoCmd); // 现有的业务逻辑
		//return deviceService.getPageDeviceList(deviceInfoCmd); // 原先的业务逻辑
	}

	@ApiOperation(value = "设备状态数量统计", tags = "门禁设备管理")
	@PostMapping("/statistics")
	public SingleResponse<SummaryDTO> statistics(@RequestBody @Validated DeviceInfoCmd deviceInfoCmd) {
		return deviceService.statistics(deviceInfoCmd);
	}

	@ApiOperation(value = "启用状态修改", tags = "门禁设备管理")
	@PostMapping("/enable/{deviceId}")
	public SingleResponse<Long> modifyStatus(@PathVariable("deviceId") Long deviceId) {
		return deviceService.modifyStatus(deviceId);
	}

	@ApiOperation(value = "编辑", tags = "门禁设备管理")
	@PostMapping("/update")
	public Response update(@RequestBody @Validated DeviceUpdateCmd deviceUpdateCmd) {
		return deviceService.update(deviceUpdateCmd);
	}

	@ApiOperation(value = "开门-常开-常闭-关闭", tags = "门禁设备管理")
	@PostMapping("/operate/door")
	public Response operateDoor(@RequestBody @Validated DeviceOperateDoorCmd deviceOperateDoorCmd, HttpServletRequest request) {
		deviceOperateDoorCmd.setIp(IpUtil.getIpAddress(request));
		return deviceService.operateDoor(deviceOperateDoorCmd);
	}

	@ApiOperation(value = "高级按钮 ", tags = "门禁设备管理")
	@PostMapping("/curfewTime")
	public Response curfewTime(@RequestBody @Validated DeviceCurfewTimeCmd deviceCurfewTimeCmd) throws ParseException {
		return deviceService.curfewTime(deviceCurfewTimeCmd);
	}

	@ApiOperation(value = "人脸识别门禁开关按钮", tags = "门禁设备管理")
	@PostMapping("/curfewOpenOrClose")
	public Response curfewOpenOrClose(@RequestBody @Validated DeviceCurfewOpenOrCloseCmd deviceCurfewOpenOrCloseCmd) throws ParseException {
		return deviceService.curfewOpenOrClose(deviceCurfewOpenOrCloseCmd);
	}

}
